package com.matrixxun.zoominlist.adapter;

import com.matrixxun.zoominlist.ResourceTable;
import ohos.agp.components.*;
import ohos.app.Context;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

/**
 * 列表provider
 */
public class ListItemProvider extends BaseItemProvider {
    private static final HiLogLabel LOG_LABEL = new HiLogLabel(HiLog.LOG_APP, 0x20221, "ListItemProvider");
    String[] mItems;
    Context mContext;
    int mXmlId;

    public ListItemProvider(Context context, String[] items, int layoutId) {
        mItems = items;
        mContext = context;
        mXmlId = layoutId;
    }

    @Override
    public int getCount() {
        return mItems.length;
    }

    @Override
    public Object getItem(int item) {
        return null;
    }

    @Override
    public long getItemId(int id) {
        return id;
    }

    @Override
    public Component getComponent(int index, Component component, ComponentContainer componentContainer) {
        ViewHolder holder;
        if (component == null) {
            component = LayoutScatter.getInstance(mContext).parse(mXmlId, componentContainer, false);
            holder = new ViewHolder();
            holder.text = (Text) component.findComponentById(ResourceTable.Id_item_text);
            component.setTag(holder);
        } else {
            holder = (ViewHolder) component.getTag();
        }
        HiLog.info(LOG_LABEL, "text : " + index + ":" + mItems[index]);
        holder.text.setText(mItems[index]);
        return component;
    }

    /**
     * ViewHolder
     */
    public static final class ViewHolder {
        Text text;
    }
}
